# 存储到redis

import sys
sys.path.append('../')

'''
mongoDB 到处excel

'''

'''
查询当日时间条件下没有存在手机号码的数据
db.phone.find({"date":"20191024","phone":{"$ne":""}})

'''

import pymongo, csv,os,time

class GuideExcel( object ):

    def __init__(self):

        self.mongo = pymongo.MongoClient('mongodb://localhost:27017/')

        self.db = self.mongo['taobao']

        self.date = time.strftime('%Y-%m-%d', time.localtime(time.time())).replace('-', '')

    # 获取mongoDB信息
    def mongoClient(self):

        # data = self.db['phone'].find({"date":'{0}'.format( self.date)})

        # data = self.db['phone'].find({"date":"{0}".format(self.date),{"phone":{"$ne":""}})

        data = self.db['phone'].find({"date":"20191024","phone":{"$ne":""}})


        for item in data:

            # 定义大列表
            res = []
            # 定义行列表
            rows = []
            # 取出_id
            item.pop('_id')
            item.pop('date')

            for row in item.values():

                rows.append( row )

            res.append( rows )

            yield res

    def writeExcel(self):



        # 创建表头
        save_file = '导出结果'

        if not os.path.exists(save_file ):

            os.mkdir( save_file )

        if not os.path.exists('导出结果/{0}.csv'.format( str(self.date ))):
            # 创建表头
            with open(r'{0}/{1}.csv'.format(save_file, str(self.date)), mode='a+', newline='') as csv_file:

                writer = csv.writer(csv_file)

                writer.writerows([["店铺UID","商家所在地","店铺旺旺昵称","商家电话","店铺标题","采集日期"]])

                print('create table header for successful')

        for item in self.mongoClient():
            # 准备写入csv
            with open(r'{0}/{1}.csv'.format(save_file, str(self.date)), mode='a+', newline='') as csv_file:

                writer = csv.writer(csv_file)
                writer.writerows( item )

                print('write csv success for %s'%item[0][4])




if __name__ == '__main__':

    g = GuideExcel()

    g.writeExcel()
    #
    # for item in g.mongoClient():
    #
    #
    #     print( item )




#
# max_list = []
#
# def readExcel():
#
#     wb = load_workbook("./Application/采集结果.xlsx")
#     # 获取所有表格(worksheet)的名字
#     sheets = wb.sheetnames
#     # 第一个表格的名称
#     sheet_first = sheets[0]
#     # 获取特定的worksheet
#     ws = wb[sheet_first]
#
#     # 获取表格所有行和列，两者都是可迭代的
#     rows = ws.rows
#     # columns = ws.columns
#
#     # 迭代所有的行
#     for row in rows:
#
#         line = [col.value for col in row]
#
#         if None in line or Helper.lenRedis() >=1000:
#
#             break
#
#         item = dict(nick=line[0], uid=line[1], city=line[2], shopId=line[3])
#
#         Helper.saveRedis( item )


# if __name__ == '__main__':
#
#     readExcel()